﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace TGM
{
    public class MainForm : Form
    {
        private readonly Engine engine = new Engine();
        private Button AgainButton;
        private Button ClearButton;
        private TextBox FamilyTextBox;
        private TextBox IntervalTextBox;
        private ListView ItemListView;
        private Button ItemSaveButton;
        private Button LoadItemsButton;
        private TextBox LogTextBox;
        private TextBox PassTextBox;
        private Timer PickTimer;
        private PictureBox RegPictureBox;
        private Button SubmitButton;
        private TextBox UserTextBox;
        private ColumnHeader columnHeader1;
        private ColumnHeader columnHeader2;
        private ColumnHeader columnHeader3;
        private IContainer components;
        private FlowLayoutPanel flowLayoutPanel1;
        private ItemConfig itemConfig;
        private Label label1;
        private Label label2;
        private Label label3;
        private TabControl tabControl1;
        private TabPage tabPage1;
        private TabPage tabPage2;
        private Label 家族名;

        public MainForm()
        {
            InitializeComponent();
        }

        private void AgainButtonClick(object sender, EventArgs e)
        {
            engine.Step5();
        }

        private void BindItems()
        {
            ItemListView.Items.Clear();
            itemConfig = ItemConfig.Load();
            if (itemConfig != null)
            {
                foreach (Item item in itemConfig.Items)
                {
                    ItemListView.Items.Add(new ListViewItem(new[] {item.Code, item.Name, item.Qty.ToString()}));
                }
            }
        }

        private void ClearButtonClick(object sender, EventArgs e)
        {
            LogTextBox.Text = string.Empty;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            components = new Container();
            SubmitButton = new Button();
            UserTextBox = new TextBox();
            PassTextBox = new TextBox();
            RegPictureBox = new PictureBox();
            label1 = new Label();
            label2 = new Label();
            LogTextBox = new TextBox();
            AgainButton = new Button();
            tabControl1 = new TabControl();
            tabPage1 = new TabPage();
            label3 = new Label();
            IntervalTextBox = new TextBox();
            家族名 = new Label();
            FamilyTextBox = new TextBox();
            ClearButton = new Button();
            tabPage2 = new TabPage();
            flowLayoutPanel1 = new FlowLayoutPanel();
            ItemSaveButton = new Button();
            LoadItemsButton = new Button();
            PickTimer = new Timer(components);
            ItemListView = new ListView();
            columnHeader1 = new ColumnHeader();
            columnHeader2 = new ColumnHeader();
            columnHeader3 = new ColumnHeader();
            ((ISupportInitialize) RegPictureBox).BeginInit();
            tabControl1.SuspendLayout();
            tabPage1.SuspendLayout();
            tabPage2.SuspendLayout();
            flowLayoutPanel1.SuspendLayout();
            base.SuspendLayout();
            SubmitButton.Location = new Point(410, 280);
            SubmitButton.Name = "SubmitButton";
            SubmitButton.Size = new Size(0x4b, 0x17);
            SubmitButton.TabIndex = 0;
            SubmitButton.Text = "Submit";
            SubmitButton.UseVisualStyleBackColor = true;
            SubmitButton.Click += SubmitButtonClick;
            UserTextBox.Location = new Point(0x40, 0x1c);
            UserTextBox.Name = "UserTextBox";
            UserTextBox.Size = new Size(0x70, 0x15);
            UserTextBox.TabIndex = 1;
            PassTextBox.Location = new Point(230, 0x1c);
            PassTextBox.Name = "PassTextBox";
            PassTextBox.Size = new Size(100, 0x15);
            PassTextBox.TabIndex = 2;
            RegPictureBox.Location = new Point(0x150, 0x12);
            RegPictureBox.Name = "RegPictureBox";
            RegPictureBox.Size = new Size(0x95, 50);
            RegPictureBox.TabIndex = 3;
            RegPictureBox.TabStop = false;
            label1.AutoSize = true;
            label1.Location = new Point(0x1d, 0x1f);
            label1.Name = "label1";
            label1.Size = new Size(0x1d, 12);
            label1.TabIndex = 4;
            label1.Text = "user";
            label2.AutoSize = true;
            label2.Location = new Point(0xc3, 0x1f);
            label2.Name = "label2";
            label2.Size = new Size(0x1d, 12);
            label2.TabIndex = 5;
            label2.Text = "pass";
            LogTextBox.Location = new Point(0x1f, 0x52);
            LogTextBox.Multiline = true;
            LogTextBox.Name = "LogTextBox";
            LogTextBox.ScrollBars = ScrollBars.Both;
            LogTextBox.Size = new Size(0x175, 0xdf);
            LogTextBox.TabIndex = 6;
            AgainButton.Enabled = false;
            AgainButton.Location = new Point(410, 0xf9);
            AgainButton.Name = "AgainButton";
            AgainButton.Size = new Size(0x4b, 0x19);
            AgainButton.TabIndex = 7;
            AgainButton.Text = "Again";
            AgainButton.UseVisualStyleBackColor = true;
            AgainButton.Click += AgainButtonClick;
            tabControl1.Controls.Add(tabPage1);
            tabControl1.Controls.Add(tabPage2);
            tabControl1.Dock = DockStyle.Fill;
            tabControl1.Location = new Point(0, 0);
            tabControl1.Name = "tabControl1";
            tabControl1.SelectedIndex = 0;
            tabControl1.Size = new Size(0x20e, 0x151);
            tabControl1.TabIndex = 8;
            tabPage1.Controls.Add(label3);
            tabPage1.Controls.Add(IntervalTextBox);
            tabPage1.Controls.Add(家族名);
            tabPage1.Controls.Add(FamilyTextBox);
            tabPage1.Controls.Add(ClearButton);
            tabPage1.Controls.Add(LogTextBox);
            tabPage1.Controls.Add(AgainButton);
            tabPage1.Controls.Add(SubmitButton);
            tabPage1.Controls.Add(UserTextBox);
            tabPage1.Controls.Add(label2);
            tabPage1.Controls.Add(PassTextBox);
            tabPage1.Controls.Add(label1);
            tabPage1.Controls.Add(RegPictureBox);
            tabPage1.Location = new Point(4, 0x16);
            tabPage1.Name = "tabPage1";
            tabPage1.Padding = new Padding(3);
            tabPage1.Size = new Size(0x206, 0x137);
            tabPage1.TabIndex = 0;
            tabPage1.Text = "Page 1";
            tabPage1.UseVisualStyleBackColor = true;
            label3.AutoSize = true;
            label3.Location = new Point(410, 0x91);
            label3.Name = "label3";
            label3.Size = new Size(0x59, 12);
            label3.TabIndex = 12;
            label3.Text = "领取间隔（秒）";
            IntervalTextBox.Location = new Point(0x19c, 0xab);
            IntervalTextBox.Name = "IntervalTextBox";
            IntervalTextBox.Size = new Size(100, 0x15);
            IntervalTextBox.TabIndex = 11;
            IntervalTextBox.Text = "30";
            IntervalTextBox.TextChanged += IntervalTextBoxTextChanged;
            家族名.AutoSize = true;
            家族名.Location = new Point(0x1d, 0x3a);
            家族名.Name = "家族名";
            家族名.Size = new Size(0x29, 12);
            家族名.TabIndex = 10;
            家族名.Text = "家族名";
            FamilyTextBox.Location = new Point(0x4c, 0x37);
            FamilyTextBox.Name = "FamilyTextBox";
            FamilyTextBox.Size = new Size(100, 0x15);
            FamilyTextBox.TabIndex = 9;
            ClearButton.Location = new Point(410, 220);
            ClearButton.Name = "ClearButton";
            ClearButton.Size = new Size(0x4b, 0x17);
            ClearButton.TabIndex = 8;
            ClearButton.Text = "Clear";
            ClearButton.UseVisualStyleBackColor = true;
            ClearButton.Click += ClearButtonClick;
            tabPage2.Controls.Add(flowLayoutPanel1);
            tabPage2.Location = new Point(4, 0x16);
            tabPage2.Name = "tabPage2";
            tabPage2.Size = new Size(0x206, 0x137);
            tabPage2.TabIndex = 1;
            tabPage2.Text = "Items";
            tabPage2.UseVisualStyleBackColor = true;
            flowLayoutPanel1.Controls.Add(ItemSaveButton);
            flowLayoutPanel1.Controls.Add(LoadItemsButton);
            flowLayoutPanel1.Controls.Add(ItemListView);
            flowLayoutPanel1.Dock = DockStyle.Fill;
            flowLayoutPanel1.Location = new Point(0, 0);
            flowLayoutPanel1.Name = "flowLayoutPanel1";
            flowLayoutPanel1.Size = new Size(0x206, 0x137);
            flowLayoutPanel1.TabIndex = 3;
            ItemSaveButton.Location = new Point(3, 3);
            ItemSaveButton.Name = "ItemSaveButton";
            ItemSaveButton.Size = new Size(0x4b, 0x17);
            ItemSaveButton.TabIndex = 0;
            ItemSaveButton.Text = "Save";
            ItemSaveButton.UseVisualStyleBackColor = true;
            ItemSaveButton.Click += ItemSaveButtonClick;
            LoadItemsButton.Location = new Point(0x54, 3);
            LoadItemsButton.Name = "LoadItemsButton";
            LoadItemsButton.Size = new Size(0x4b, 0x17);
            LoadItemsButton.TabIndex = 1;
            LoadItemsButton.Text = "Load";
            LoadItemsButton.UseVisualStyleBackColor = true;
            LoadItemsButton.Click += LoadItemsButtonClick;
            PickTimer.Interval = 0x7530;
            PickTimer.Tick += PickTimerTick;
            ItemListView.Columns.AddRange(new[] {columnHeader1, columnHeader2, columnHeader3});
            ItemListView.FullRowSelect = true;
            ItemListView.Location = new Point(0xa5, 3);
            ItemListView.Name = "ItemListView";
            ItemListView.Size = new Size(0x14f, 280);
            ItemListView.TabIndex = 2;
            ItemListView.UseCompatibleStateImageBehavior = false;
            ItemListView.View = View.Details;
            columnHeader1.Text = "Code";
            columnHeader2.Text = "Name";
            columnHeader3.Text = "Qty";
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x20e, 0x151);
            base.Controls.Add(tabControl1);
            base.HelpButton = true;
            base.Name = "MainForm";
            Text = "自动领东西-不懂请按F1，";
            base.Load += MainFormLoad;
            base.HelpRequested += MainFormHelpRequested;
            ((ISupportInitialize) RegPictureBox).EndInit();
            tabControl1.ResumeLayout(false);
            tabPage1.ResumeLayout(false);
            tabPage1.PerformLayout();
            tabPage2.ResumeLayout(false);
            flowLayoutPanel1.ResumeLayout(false);
            base.ResumeLayout(false);
        }

        private void IntervalTextBoxTextChanged(object sender, EventArgs e)
        {
            PickTimer.Interval = int.Parse(IntervalTextBox.Text)*0x3e8;
        }

        private void ItemSaveButtonClick(object sender, EventArgs e)
        {
            if (itemConfig == null)
            {
                itemConfig = new ItemConfig();
                itemConfig.Items.Add(new Item("20545", "大全能", 0));
            }
            itemConfig.Save();
        }

        private void LoadItemsButtonClick(object sender, EventArgs e)
        {
            BindItems();
        }

        private void MainFormHelpRequested(object sender, HelpEventArgs hlpevent)
        {
            new AboutForm().ShowDialog(this);
        }

        private void MainFormLoad(object sender, EventArgs e)
        {
            BindItems();
        }

        public void OutputMsg(string message)
        {
            LogTextBox.Text = LogTextBox.Text + message;
        }

        private void PickTimerTick(object sender, EventArgs e)
        {
            engine.Step5();
        }

        public void SetRegImge(Image image)
        {
            RegPictureBox.Image = image;
            Refresh();
        }

        private void SubmitButtonClick(object sender, EventArgs e)
        {
            engine.ItemConfig = itemConfig;
            engine.Owner = this;
            engine.User = UserTextBox.Text;
            engine.Pass = PassTextBox.Text;
            engine.Execute();
            PickTimer.Enabled = true;
        }
    }
}